﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using System.Runtime.InteropServices;

namespace TeenyGrab.Inputs
{
    /// <summary>
    /// Interaction logic for ConfigureWindowInputWindow.xaml
    /// </summary>
    public partial class ConfigureWindowInputWindow : Window
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 
        
        private MouseHookListener MouseHook { get; set; }
        private IntPtr Window { get; set; }

        private WindowInput Input { get; set; }

        public ConfigureWindowInputWindow(WindowInput input)
        {
            InitializeComponent();

            Input = input;

            Window = Input.Window;
            if (Window != null) { UpdateDisplay(); }
            radioButtonForeground.IsChecked = input.ForegroundWindow; 
            radioButtonSpecific.IsChecked = !input.ForegroundWindow;

            MouseHook = new MouseHookListener(new GlobalHooker());
            MouseHook.MouseUp += new System.Windows.Forms.MouseEventHandler(MouseHook_MouseUp);
        }

        void MouseHook_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            MouseHook.Enabled = false;
            Window = GetForegroundWindow();

            buttonRefresh.IsEnabled = (radioButtonSpecific.IsChecked == true) && (Window != null);
            UpdateDisplay();
        }

        private void UpdateDisplay()
        {
            var title = new StringBuilder();
            GetWindowText(Window, title, int.MaxValue);
            textBlockWindowDetails.Text = title.ToString();
        }

        private void radioButtonSpecific_Checked(object sender, RoutedEventArgs e)
        {
            buttonPick.IsEnabled = radioButtonSpecific.IsChecked == true; 
            buttonRefresh.IsEnabled = (radioButtonSpecific.IsChecked == true) && (Window != null);
        }

        private void buttonPick_Click(object sender, RoutedEventArgs e)
        {
            textBlockWindowDetails.Text = "Click on a window to select it.";
            MouseHook.Enabled = true;
        }

        private void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            if (MouseHook.Enabled == true)
                return;
            this.DialogResult = true;
            Input.ForegroundWindow = radioButtonForeground.IsChecked == true;
            Input.Window = Window;
            this.Close();
        }

        private void radioButtonForeground_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            if (Window != null)
                UpdateDisplay();
        }
    }
}
